package edu.gatech.bsbmule.model;

import com.google.gson.Gson;


/**
 * This class represents the store where player can buy/sell items.
 * 
 * @author Melissa, Vania, Manav
 * version 1.0 10/20/13
 */
public class Store {
	private int[][] resources = new int[4][2];
	private int basePriceMule;
	private int feeFoodMule;
	private int feeEnergyMule;
	private int feeSmithoreMule;
	private int priceFood;
	private int priceEnergy;
	private int priceSmithore;
	private int typeOfMule; 
	private int finalPrice; 
	
	public static final int RESOURCE_MULE = 0;
	public static final int RESOURCE_FOOD = 1;
	public static final int RESOURCE_ENERGY = 2;
	public static final int RESOURCE_SMITHORE = 3;
	public static final int FOOD_MULE = 4;
	public static final int ENERGY_MULE = 5;
	public static final int SMITHORE_MULE = 6;
	
	private static Store instance;
	private static final Gson gson = new Gson();
	
	/**
	 * Get instance method
	 * 
	 * @return instance
	 */
	public static Store getInstance() {
		if (instance == null) {
			instance = new Store();
		}
		return instance;
	}
	
	/**
	 * Store constructor
	 */
	private Store() {
		this.basePriceMule = 100;
		this.feeFoodMule = 25;
		this.feeEnergyMule = 50;
		this.feeSmithoreMule = 75;
		this.priceFood = 30;
		this.priceEnergy = 25;
		this.priceSmithore = 50;
		
		resources[0][0] = RESOURCE_MULE;
		resources[1][0] = RESOURCE_FOOD;
		resources[2][0] = RESOURCE_ENERGY;
		resources[3][0] = RESOURCE_SMITHORE;
		
		//initial store quantities
		resources[0][1] = 14; 
		resources[1][1] = 8;
		resources[2][1] = 8;
		resources[3][1] = 8;		
	}
	
	/**
	 * Gets price of a resource 
	 * 
	 * @return price
	 */
	public int getPrice(int resource) {		
		if (resource == 1) {
			finalPrice = priceFood; 
			return finalPrice; 
		} else if (resource == 2) {
			finalPrice = priceEnergy;
			return finalPrice;	
		} else if (resource == 3) {
			finalPrice = priceSmithore;
			return finalPrice; 
		} else if (resource == 4) {
			return getMulePrice(4);
		} else if (resource == 5) {
			return getMulePrice(5);
		} else {
			return getMulePrice(6);
		}	
	}
		
	/**
	 * Gets name of resource
	 * 
	 * @param int resource
	 * @return resource name
	 */
	public String getResourceName(int resource) {
		if (resource == 0) {
			return "Mule";
		} else if (resource == 1) {
			return "Food";
		} else if (resource == 2) {
			return "Energy";
		} else if (resource == 3) {
			return "Smithore";
		} else if (resource == 4) {
			return "Food Mule"; 
		} else if (resource == 5) {
			return "Energy Mule";	
		} else {
			return "Smithore Mule";
		}
	}

	/**
	 * Gets quantity of item available in store
	 * 
	 * @param int resource
	 * @return quantity
	 */
	public int getItemQuantity(int resource) {
		return resources[resource][1]; 
	}
	
	/**
	 * Gets array of resources
	 * 
	 * @return resources
	 */
	public int[][] getResources() {
		return resources; 
	}
	
	/**
	 * Sets mule type
	 * 
	 * @param int typeOfMule
	 */
	public void setMuleType(int typeOfMule) {
		this.typeOfMule = typeOfMule;		
	}
	
	/**
	 * Gets mule price based on type
	 * 
	 * @param int muleType
	 * @return price
	 */
	public int getMulePrice(int muleType) {
		if (typeOfMule == 4) {
			finalPrice = basePriceMule + feeFoodMule; 
			return finalPrice;		
		} else if (typeOfMule == 5) {
			finalPrice = basePriceMule + feeEnergyMule;
			return finalPrice; 
		} else {
			finalPrice = basePriceMule + feeSmithoreMule;
			return finalPrice; 
		}
	}
	
	/**
	 * Check if player has enough money for a purchase
	 * 
	 * @param Player player
	 * @param int price
	 * @return true if player has enough money
	 */
	public boolean hasMoney(Player player, int price) {
		int playerMoney = player.getMoney();
		return (playerMoney >= price);		
	}
	
	/**
	 * Sells resource to a player
	 * 
	 * @param Player player
	 * @param int resource
	 * @param int quantity
	 */
	public void sellResource(Player player, int resource, int quantity) {
		player.subtractMoney(getPrice(resource));
		
		if (resource == 1) {
			resources[1][1] -= quantity;
			player.addFood(quantity);			
		} else if (resource == 2) {
			resources[2][1] -= quantity;
			player.addEnergy(quantity);
		} else if (resource ==3) {
			resources[3][1] -= quantity;
			player.addSmithore(quantity);
		} else if (resource  == 4) {
			resources[0][1] -= quantity; 
			player.setMule(4);
		} else if (resource == 5) {
			resources[0][1] -= quantity; 
			player.setMule(5);
		} else {
			resources[0][1] -= quantity; 
			player.setMule(6);
		}
	}
	
	/**
	 * Displays the resources that a player can sell
	 */   
		public void displaySellableResources(Player p){
			int food = p.getFood();
			int energy = p.getEnergy();
			int smithore = p.getSmithore();
		
			System.out.println("Food that can be sold: "+ food);
			System.out.println("Energy that can be sold: " + energy);
			System.out.println("Smithore that can be sold: " + smithore);
		}

	/**
	 * Store buys a quantity of a resource from a player
	 * 	
	 * @param Player p
	 * @param  int resource
	 * @param int quantity
	 */
	  public void buy(Player p, int resource, int quantity){
		  int moneyEarned;
		  if  (resource >=1 && resource <=3) {
			 moneyEarned = getPrice(resource)*quantity;
			 p.addMoney(moneyEarned);

			 if(resource == 1){
				 p.setFood(p.getFood() - quantity);				 
			 } else if ( resource ==2 ) {
				 p.setEnergy(p.getEnergy() - quantity);
			 } else if (resource == 3) {
				 p.setSmithore(p.getSmithore() - quantity);
			 }
		  }  
	  }
	  
	  public static String saveToJson() {
		  return gson.toJson(instance);
	  }
	  
	  public static void loadFromJson(String json) {
		  instance = gson.fromJson(json, Store.class);
	  }
}
